package org.iplatform.felicajack;

import org.iplatform.felicajack.mfcaccess.FelicaServiceConnection;
import org.iplatform.felicajack.mfcaccess.MFCSampleUse;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.felicanetworks.mfc.AppInfo;
import com.felicanetworks.mfc.FelicaEventListener;

public class PUserAddActivity extends Activity implements FelicaEventListener {
	private final Button usernameSendButton = null;
	private EditText userEditText = null;
	private boolean condition = false;
	private boolean activateFlag = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.puser_add_layout);
        userEditText = (EditText)findViewById(R.id.EditText01);

        final MFCSampleUse mfc = MFCSampleUse.getInstance();
        mfc.setContext(this);

        findViewById(R.id.send).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				try {
					new Thread(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							sendUserId(userEditText.getText().toString());
						}
					}).start();
				} catch (final Exception e) {
					// TODO: handle exception
				}

//				mfc.pushToAddUserIntent(userEditText.getText().toString());
			}
		});

        final FelicaServiceConnection felicaConnection = FelicaServiceConnection.getInstance();
        boolean connected = false;
        try {
            // Felica�ւ̐ڑ�
            felicaConnection.setContext(this);
            felicaConnection.connect();

            connected = true;
        } catch (final Exception e) {
            // �ڑ��v���Ɏ��s�����ꍇ�A�G���[���b�Z�[�W��\�����ďI��
            Toast.makeText(this, "Error********", Toast.LENGTH_LONG);
            return;
        } finally {
            if (!connected) {
                try {
                    felicaConnection.disconnect();
                } catch (final Exception e) {
                    // ��������
                }
            }
        }
    }

    synchronized private void sendUserId(final String string) {
    	final MFCSampleUse mfc = MFCSampleUse.getInstance();
    	mfc.setContext(this);
    	mfc.activateFelica(this);
        condition = false;
        waitEvent();

        if (!activateFlag) {
        	return;
        }
    	mfc.open();

    	final boolean isSuccess = mfc.pushToAddUserIntent(string);
    	Log.d("IS_SUCCESS", String.valueOf(isSuccess));

    	mfc.close();
    	mfc.inactivateFelica();
	}

    synchronized void waitEvent(){
        while(!condition){
            try {
                wait();
            } catch(final Exception e){

            }
        }
    }

    // activateFelica()Çé¿çsÇµÇΩåãâ ÅAÉGÉâÅ[Ç™î≠ê∂ÇµÇΩèÍçá
    public void errorOccurred(final int i, final String s, final AppInfo appinfo) {

         activateFlag = false;

         wakeUp();

     }

    // activateFelica()Çé¿çsÇµÇΩåãâ ÅAê≥èÌÇ…èIóπÇµÇΩèÍçá
     public void finished() {

         activateFlag = true;

         wakeUp();

     }

     synchronized void wakeUp(){
         condition = true;
         notify();
     }


}